/**
 * Copyright 2014 opencxa.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cxa.rpc.cloud.server.components.authentication;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;
import org.cxa.misc.sqLiteUtils.SqLiteTableColumn;
import org.cxa.misc.sqLiteUtils.SqLiteTableColumn.DataType;
import org.cxa.misc.sqLiteUtils.SqLiteTableColumn.Modifier;
import org.cxa.misc.stringUtils.DelimitedStringBuilder;

import com.google.gson.annotations.Expose;

/**
 * @author Christopher Armenio
 *
 */
public class User
{
	public static final String FIELD_NAME_EMAIL = "email";
	
	
	@SqLiteTableColumn(dataType = DataType.INTEGER, modifiers = {Modifier.PRIMARY_KEY, Modifier.AUTO_INCREMENT, Modifier.NOT_NULL}) @Expose 
	private Integer id;
	@SqLiteTableColumn(dataType = DataType.TEXT, modifiers = {Modifier.NOT_NULL}) @Expose
	private String email;
	@SqLiteTableColumn(dataType = DataType.TEXT, modifiers = {}) @Expose
	private String groups;
	@SqLiteTableColumn(dataType = DataType.BOOLEAN, modifiers = {Modifier.NOT_NULL}) @Expose
	private Boolean isEnabled;
	@SqLiteTableColumn(dataType = DataType.TEXT, modifiers = {Modifier.NOT_NULL}) @Expose
	private String passwordHash;
	@SqLiteTableColumn(dataType = DataType.TEXT, modifiers = {Modifier.NOT_NULL}) @Expose
	private String passwordSalt;
	
	
	/*
	 * required for SqLiteTableColumn instantiation 
	 */
	public User() { }
	
	
	public User(String emailIn, String passwordIn, Integer[] groupsIn, Boolean isEnabledIn)
	{
		this.id = null;
		this.email = emailIn;
		this.passwordSalt = UUID.randomUUID().toString();
		this.passwordHash = generatePasswordHash(passwordIn, this.passwordSalt);
		this.isEnabled = isEnabledIn;
		this.groups = getGroupString(groupsIn);
	}
	
	
	public Integer getId()
	{
		return this.id;
	}
	
	
	public String getEmail()
	{
		return this.email;
	}
	
	
	public List<Integer> getGroups()
	{
		List<Integer> retVal = new ArrayList<Integer>();
		if( this.groups == null ) return retVal;
		
		for( String currGroup : this.groups.split(",") )
		{
			retVal.add(Integer.parseInt(currGroup.trim()));
		}
		
		return retVal;
	}
	
	
	public void setGroups(Integer[] groupsIn)
	{
		this.groups = getGroupString(groupsIn);
	}
	
	
	public Boolean isEnabled()
	{
		return this.isEnabled;
	}
	
	
	public String getPasswordHash()
	{
		return this.passwordHash;
	}
	
	
	public String getPasswordSalt()
	{
		return this.passwordSalt;
	}
	
	
	@Override
    public boolean equals(Object objectIn)
    {
		return (objectIn instanceof User) ? (((User)objectIn).id == this.id) : false;
    }
	
	
	public static String generatePasswordHash(String passwordIn, String saltIn)
	{
		return DigestUtils.sha256Hex(saltIn+passwordIn);
	}
	
	
	private static String getGroupString(Integer groupsIn[])
	{
		DelimitedStringBuilder dsb = new DelimitedStringBuilder(", ");
		if( groupsIn != null )
		{
			for( Integer currGroup : groupsIn )
			{
				dsb.append(currGroup.toString());
			}
		}
		return dsb.toString();
	}
}
